import api from "../../../api/api.js"
import consts from "../../../utils/consts.js"
import courseUtil from "../../../utils/course.js"

Page({
  data: {
    colorArrays: ["#85B8CF", "#90C652", "#D8AA5A", "#FC9F9D", "#0A9A84", "#61BC69", "#12AEF3", "#E29AAD"],
    weeks: consts.weeks,
    timeList: [],
    th: 150,
    courses: []
  },
  onLoad: function (option) {
    var that = this
    api.courses({
      method: 'get',
      data: {
        openid: getApp().globalData.openid
      },
      success: function (resp) {
        var courses = courseUtil.parseCourses(resp.data)
        courseUtil.setCourses(courses)
        that.setData({
          courses: courses
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var startTime = getApp().globalData.userConf.startTime
    var endTime = getApp().globalData.userConf.endTime
    var tableHeight = getApp().globalData.userConf.tableHeight
    if (startTime && endTime) {
      this.setData({
        timeList: consts.timeList.slice(startTime, endTime),
        th: tableHeight
      })
    } else {
      this.setData({
        timeList: consts.timeList.slice(9, 19)
      })
    }
    this.setData({
      courses: courseUtil.getCourses()
    })
  },

  bindAddCourseTap: function () {
    wx.navigateTo({
      url: '/pages/my/course-edit/index',
    })
  }
})